Memory pools, also known as fixed-size blocks allocation, use pools for memory management enabling efficient dynamic memory allocation. Unlike standard methods such as malloc or C++’s operator new, which can suffer from fragmentation and are unsuitable for real-time systems, memory pools preallocate fixed-size blocks that can be allocated and freed quickly using handles at run time. Many real-time operating systems, like the Transaction Processing Facility, utilize memory pools. Additionally, systems such as the web server Nginx use 'memory pool' to mean a group of variable-size allocations freed together, known as region-based memory management.
Simple memory pool implementation
A simple memory pool module can allocate, for example, three pools at compile time with block sizes optimized for the application deploying the module. The application can allocate, access and free memory through the following interface:
- Allocate memory from the pools. The function will determine the pool where the required block fits in. If all blocks of that pool are already reserved, the function tries to find one in the next bigger pool(s). An allocated memory block is represented with a handle.
- Get an access pointer to the allocated memory.
- Free the formerly allocated memory block.
- The handle can for example be implemented with an unsigned int. The module can interpret the handle internally by dividing it into pool index, memory block index and a version. The pool and memory block index allow fast access to the corresponding block with the handle, while the version, which is incremented at each new allocation, allows detection of handles whose memory block is already freed (caused by handles retained too long).
Memory pool vs malloc
Benefits
- Memory pools allow memory allocation with constant execution time. The memory release for thousands of objects in a pool is just one operation, not one by one if malloc is used to allocate memory for each object.
- Memory pools can be grouped in hierarchical tree structures, which is suitable for special programming structures like loops and recursions.
- Fixed-size block memory pools do not need to store allocation metadata for each allocation, describing characteristics like the size of the allocated block. Particularly for small allocations, this provides substantial space savings.
- Allows deterministic behavior on real-time systems by avoiding out of memory errors.
Drawbacks
- Memory pools may need to be tuned for the application which deploys them.